package com.payment;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.payment.common.utils.DateUtils;
import com.payment.system.service.ITaskService;

@Component
public class Task {
	// 是否启用所有定时任务,在本地的时候可以关闭
	final static boolean IS_ACTIVITE = false;

	@Autowired
	private ITaskService taskService;

	/**
	 * 养卡明细插入队列 开启
	 *
	 * @throws InterruptedException
	 */
	@Scheduled(cron = "* * 9-22 * * ?")
	public void zdplanorder() throws InterruptedException {
		if (IS_ACTIVITE) {
			taskService.zdplanorder();
		}
	}

	/**
	 * 读取队列中的计划进行执行 （开启）
	 *
	 * @throws InterruptedException
	 */
	@Scheduled(cron = "* * 9-22 * * ?")
	public void implementationPlan() {
		if (IS_ACTIVITE) {
			taskService.implementationPlan();
		}
	}

	/**
	 * 随时扫描
	 */
	@Scheduled(cron = "* * * * * ?")
	public void partner() {
		if (IS_ACTIVITE) {
			DateFormat df = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS);
			Calendar day = Calendar.getInstance();
			day.setTime(new Date());
			day.add(Calendar.DATE, -7);
			taskService.partner(df.format(day.getTime()));
		}
	}

	/**
	 * 统计每天和月的用户推荐人数跟交易量 每晚23:59分统计 避免数据不够精准
	 */
	@Scheduled(cron = "0 59 23 * * ?")
	public void userStatisticsUpgrade() {
		if (IS_ACTIVITE) {
			/* 获取当天时间 */
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			// 创建时间操作对象
			Calendar time = Calendar.getInstance();
			// 设置时间为当前时间
			time.setTime(new Date());
			/* 创建时间操作对象 获取本月最后一天 */
			Calendar c = Calendar.getInstance();
			// 设为本月第一天
			c.set(Calendar.DAY_OF_MONTH, 1);
			// 月份加一
			c.add(Calendar.MONTH, 1);
			// 天数加 -1
			c.add(Calendar.DATE, -1);
			taskService.userStatistics(df.format(time.getTime()) + " 00:00:00", df.format(time.getTime()) + " 23:59:59",
					DateUtils.isSameDay(c, time));
		}
	}

	/**
	 * 删除7天前的天统计，与6个月前的月统计 23点执行
	 */
	@Scheduled(cron = "0 0 23 * * ?")
	public void fanXianUser() {
		if (IS_ACTIVITE) {
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			Calendar day = Calendar.getInstance();
			day.setTime(new Date());
			day.add(Calendar.DATE, -7);
			taskService.deleteDay(df.format(day.getTime()) + " 00:00:00");
			/* 获取6个月前的一号 */
			Calendar month = Calendar.getInstance();
			month.set(Calendar.DAY_OF_MONTH, 1);
			month.add(Calendar.MONTH, -6);
			taskService.deleteMonth(df.format(day.getTime()) + " 00:00:00");
		}
	}

	public static void main(String[] args) {
		Calendar day = Calendar.getInstance();
		day.setTime(new Date());
		day.add(Calendar.DATE, -7);
		/* 获取当天时间 */
		DateFormat df = new SimpleDateFormat(DateUtils.YYYY_MM_DD_HH_MM_SS);
		System.out.println(DateUtils.getDatePoor(new Date(), day.getTime()));
	}

}
